<template>
  <a-modal
    v-model:visible="visible"
    :title="modalTitle"
    width="65%"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-table
      :columns="columns"
      :data-source="state.data"
      rowKey="goods_id"
      :pagination="state.pagination"
      :loading="state.loading"
      :row-selection="{
        onChange: onSelectChange,
        selectedRowKeys: state.selectedRowKeys,
        type: 'radio',
      }"
      @change="pageChange"
    >
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'goods_info'">
          <div class="flex gap">
            <a-image :width="100" :src="record.goods_img" class="image" />
            <div>
              <div class="good-name">
                <a-tooltip>
                  <template #title>{{ record.goods_name }}</template>
                  {{ record.goods_name }}
                </a-tooltip>
              </div>
            </div>
          </div>
        </template>
      </template>
    </a-table>
  </a-modal>
</template>
  <script setup>
import { reactive } from "vue";
import { list } from "@/api/goods/goods";

import useModal from "@/utils/useModal";
const { visible, modalTitle } = useModal();
const emits = defineEmits(["select-goods"]);
const columns = [
  {
    title: "id",
    dataIndex: "goods_id",
    width: 100,
  },
  {
    title: "商品信息",
    dataIndex: "goods_info",
    width: 300,
  },
  {
    title: "成本价",
    dataIndex: "cost_price",
    width: 150,
    customRender(record) {
      return record.value ? "￥" + record.value : "-";
    },
  },
  {
    title: "商品价格",
    dataIndex: "goods_price",
    width: 100,
    customRender(record) {
      return record.value ? "￥" + record.value : "-";
    },
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width: 200,
  },
]; //表格展示的字段
const state = reactive({
  data: [], //表格的数据
  loading: false,
  pagination: {
    total: 0,
    showSizeChanger: true, //是否页码可以改变
    current: 1, //当前xx页面
    pageSize: 10, //一页展示xx条数据
    showTotal: function (total) {
      return "总共" + total + "条数据";
    }, //总共xx条
    showQuickJumper: true,
  }, //分页数据
  searchParams: {
    goods_type: 6,
    page: 1,
    limit: 10,
  }, //搜索数据
  selectedRowKeys: [], //选中的keys
  selectedRows: [],
});
const show = () => {
  modalTitle.value = "商品列表";
  visible.value = true;
  getList();
};

const getList = () => {
  state.loading = true;
  list({
    ...state.searchParams,
  }).then((res) => {
    if (res.code == 0) {
      state.loading = false;
      const data = res.data;
      state.data = data.data;
      state.pagination.total = data.total;
    }
  });
};
const pageChange = (pagination) => {
  state.pagination.current = pagination.current;
  state.pagination.pageSize = pagination.pageSize;

  state.searchParams.page = pagination.current;
  state.searchParams.limit = pagination.pageSize;
  getList();
};
const onSelectChange = (selectedRowKeys, selectedRows) => {
  state.selectedRowKeys = selectedRowKeys;
  state.selectedRows = selectedRows;
};
const handleOk = () => {
  if (state.selectedRows.length === 0) {
    message.error("请至少选择一件商品");
  } else {
    emits("select-goods", state.selectedRows);
    visible.value = false;
  }
};
const handleCancel = () => {
  visible.value = false;
};
defineExpose({
  show,
});
</script>
  <style>
</style>
  